function showInfo(id){
    $("#info-"+id).show("fast");
}
function hideInfo(id){
    $("#info-"+id).hide("fast");
}


function IsSelected(pClass){
    var temp = new Array();
    var temp= pClass.split(" ");
    var found= false;
			
    //Periksa flag SELECTED (class) dala
    i=0;
    while(temp[i] || temp[i+1]){
        if(temp[i]=="selected"){
            found = true;
            break;
        }
        i++;
    }
    return found;
}



function KENumericAllow (id){
    $("#"+ id).keypress(function(event) {
    
        if (event.charCode && (event.charCode < 48 || event.charCode > 57)) {
            event.preventDefault();
        }
    });
}

function selectAllCheckbox(action, id){
    if(action == true)
        $("."+ id).attr("checked", "true");
    else 
        $("."+ id).removeAttr("checked");
}





$(document).ready(function() {
    /* KEYBOARD FORM NAVIGATION */
    var KeyPress= null;
    isCtrl= false;
    var tCurrentIndex= null;
    tTargetIndex= null;
    
    
    $("select").keydown(function(event) {
        if(!isCtrl && (event.keyCode==38 || event.keyCode==40)){
            $(this).options.value;
        }
        
    });
    
    $(".formInputField").keydown(function(event) {
        //CHECK: APAKAH CTRL KEY DITEKAN
        if(event.keyCode==17){
            isCtrl= true;
        }
        
        //FILTER: KeyCode Yang Diperbolehkan
        if(event.keyCode==13 || event.keyCode==38 || event.keyCode==40  || event.keyCode==72 || event.keyCode==119 || event.keyCode==187) {
            KeyPress= event.keyCode;
            
            //DON'T ALLOW IF CTRL+NAV INPUT (NAV UP+DOWN && CTRL)
            if (event.keyCode==38 || event.keyCode==40) {
                event.preventDefault();
            }
        }
        

    }).keyup(function(event) {
        
        found= false;
        isNav= false;
        isShortcut= false;
        isSubmit= false;

        //CHECK: IS CTRL RELEASED?
        if(event.keyCode==17){
            isCtrl= false;
        }
        
        //CHECK: EKSEKUSI KEY YANG DITEKAN
        if(KeyPress!=null){
            tKey= KeyPress.toString();
            switch (KeyPress) {
                case 13:{
                    //return
                    isNav= 1;
                    break;
                }
                case 38:{
                    isNav= -1;
                    break;
                }

                case 40: {
                    isNav= 1;
                    break;
                }
                
                case 72: {
                    //h
                    isShortcut= true;
                    break;
                }
                case 119: {
                    //submit f8
                    isSubmit= true;
                    break;
                }
                case 187: {
                    //+
                    isShortcut= true;
                    break;
                }
            }
               
            
            if(isSubmit){
                $("form.Template-Form").submit();
            }
            else {
                if(isCtrl){
                    if(isNav){
                        tCurrentIndex= $(this).attr("tabindex");
                        tTargetIndex= parseInt(tCurrentIndex)+ isNav;
                        while($('.formInputField[tabindex="'+tTargetIndex+'"]').attr("type") == "caption"){
                            tTargetIndex+= isNav;
                        }

                        $('.formInputField[tabindex="'+tTargetIndex.toString() +'"]').focus();
                        $('.formInputField[tabindex="'+tTargetIndex.toString()+'"]').trigger('click');
                    }
                    else if(isShortcut){
                        id= $(this).attr("id");
                        if(KeyPress == 72){
                            if($('.Helper-Container#Helper-Container-'+id).css('display')=="none") {
                                $('.Button.Help[rel="'+id+'"]').trigger('click');
                            }
                            else {
                                $('.Helper-Container[rel="'+id+'"] .Button.HelperClose ').trigger('click');
                            }
                        }
                        else if(KeyPress==187){
                            tMultiFormCountCurrent= document.getElementById("fMultiFormCount").value;
                            tMultiFormCountTarget= parseInt(tMultiFormCountCurrent)+1;
                            
                            tCurrentIndex= $(this).parent("li").parent("ul").parent("div").attr("index");
                            tTargetIndex= parseInt(tCurrentIndex)+1;
                            
                            tTargetTabindex= $('div[index="'+tTargetIndex+'"]').children("ul").children("li.First").children(".formInputField").attr("tabindex");
                            tTargetTabindex= parseInt(tTargetTabindex);
                            
                            maxloop=100;
                            i=0;
                            while($('.formInputField[tabindex="'+tTargetTabindex+'"]').attr("type") == "caption" && i<maxloop){
                                tTargetTabindex+= 1;
                                i++;
                            }
                            
                            //SHOW (CLICK) NEW MULTIFORM
                            $('.button.MultiForm[index="'+tCurrentIndex+'"]').fadeOut('normal');
                            $('.MultiForm[index="'+tTargetIndex+'"]').fadeIn('normal');
                            
                            //SET MULTIFORM COUNT
                            document.getElementById("fMultiFormCount").value= tMultiFormCountTarget;
                            
                            $('.formInputField[tabindex="'+tTargetTabindex+'"]').focus();
                            $('.formInputField[tabindex="'+tTargetTabindex+'"]').trigger('click');
                        //$('.MultiForm.Template-Form-Container[index="'+tTargetIndex+'"]').children("ul").children("li.First").children(".formInputField").focus();
                        //$('.MultiForm.Template-Form-Container[index="'+tTargetIndex+'"]').children("ul").children("li.First").children(".formInputField").trigger("click");
                        }
                    }
                }
                else if(KeyPress == 13){
                    tCurrentIndex= $(this).attr("tabindex");
                    tTargetIndex= parseInt(tCurrentIndex)+ isNav;

                    while($('.formInputField[tabindex="'+tTargetIndex+'"]').attr("type") == "caption"){
                        tTargetIndex+= isNav;
                    }
                    $('.formInputField[tabindex="'+tTargetIndex+'"]').focus();
                    $('.formInputField[tabindex="'+tTargetIndex+'"]').trigger('click');
                }
            }
            

            //RESET KEY
            KeyPress= null;
        }
        
        
        
    });	
   
    
    $(".formInputField").click(
        function() {
            
            
            //Berikan nilai default untuk navigasi
            KeyPress= null; 
            
            id= $(this).attr('id') ;
            tClass= $(this).attr('class');
            found= IsSelected(tClass);
            tType= $(this).attr("type");
			
			
            //RESET ALL FORM INFO
            //JIKA FIELD YANG TELAH DISELECT MAKA TIDAK PERLU RESET
            if(!found) {
                $('.FormInfo-Container').children('ul').fadeOut('fast');			
            }
			
            //Hilangkan seluruh attribute selected karena akan di rebuild
            //Pengecualian untuk caption karena tidak terdapat helper
            if(tType!="caption"){
                $('.formInputField').removeClass('selected');
                $('.FormInfo-Container#' + id).children('ul').fadeIn('1000');	
                $('.FormInfo-Container#' + id).children('ul').children('li').siblings('li').children('div.FormInfo-Info').fadeIn('normal');	
                $('.FormInfo-Container#' + id).children('ul').children('li').children('div.FormInfo-Helper').fadeIn('normal');	
                $(this).addClass('selected');
            
                //RESET UKURAN HEIGHT CANVAS
                tCurHeight= $("#Page-main").css("height");
                $("#Page-main").css("min-height", tCurHeight);
            }
			
        }
        );
	
    
    $(".button.MultiForm").click(function(){
        tCurrentIndex= $(this).attr("index");
        tTargetIndex= parseInt(tCurrentIndex)+1;
        tMultiFormCount= document.getElementById("fMultiFormCount").value;
        
        $(this).fadeOut('normal');
        $('.MultiForm[index="'+tTargetIndex+'"]').fadeIn('normal');
        
        $('.MultiForm.Template-Form-Container[index="'+tTargetIndex+'"]').children("ul").children("li.First").children("input").focus();
        $('.MultiForm.Template-Form-Container[index="'+tTargetIndex+'"]').children("ul").children("li.First").children("input").trigger("click");
        
        //INCREMENT MULTIFORMCOUNT
        tMultiFormCount++;
        document.getElementById("fMultiFormCount").value= tMultiFormCount;
    });
	
    $(".Button.Help").click(
        function() {
            id= $(this).attr('rel');//$(this).parent('div').parent('div').attr('id');	
            currentWidth= $("#"+id).css('width');
            $(this).fadeOut('slow');
            
            $(".Helper-Container#Helper-Container-"+id).css('width', (parseInt(currentWidth)+28));
            $(".Helper-Container#Helper-Container-"+id).fadeIn('normal');
        }
        );
	
	
    $(".Button.HelperClose").click(
        function() {
            id= $(this).parent('div').parent('div').attr('rel');//$(this).parent('div').parent('div').attr('id');	
            $(this).parent('div').parent('div').fadeOut('slow');//$(this).parent('div').parent('div').attr('id');	
            $(".Help.Button#Help-"+id).fadeIn('normal');	
        }
        );
	
	
	
    $(".BackspaceButton").click(
        function() {
            id= $(this).attr('rel');
            document.getElementById(id).value = "";
        }
        );
	
    $(".Template-Form-Input").hover(		
        //Mouseover, fadeIn the hidden hover class	
        function() {		
            id= $(this).children('span').stop(true, true).attr('id');
            $('#' + id).fadeIn('1000');				
        }, 
	
        //Mouseout, fadeOut the hover class
        function() {
            btn_id= $(this).children('span').stop(true, true).attr('id');
            $('#' + btn_id).fadeOut('1000');		
        }).click (function () {
        $(this).addClass('selected');
    });
    
    $(".button.back").click(
        function(){
            tTarget= $(".button.back").attr("href");
            if(tTarget){
                BackId= $(".button.back").attr("rel");
                document.getElementById("fId").value= BackId;
                $("form.Template-Form").attr("action", tTarget);
                $("form.Template-Form").attr("onsubmit", "");
                $("form.Template-Form").submit();
            }
        });
        
    $(".button.submit").click(
        function(){
            $("form.Template-Form").submit();
        });
        
    $("input.DatePicker").datepicker({
        showOn			: "button",
        altFormat		: "DD, dd MM yy",
        buttonImage		: "lib/template/images/calendar.gif",
        changeMonth: true,
        changeYear: true,
        buttonImageOnly	: true 
    });
	
});
